package terra.model.object.stat;

import terra.managers.ThreadPoolManager;
import terra.model.Experience;
import terra.model.object.LiveObject;
import terra.model.object.Player;

public class PlayerStat extends CharacterStat {

	private static final long serialVersionUID = 1L;
	public PlayerStat(LiveObject liveObject) {
		super(liveObject);
	}

	@Override
	public final boolean addLevel(byte value) {
		if (getLevel() + value > Experience.MAX_LEVEL - 1)
			return false;
		boolean levelIncreased = super.addLevel(value);
		if (levelIncreased) {
			setCurrentHp(getMaxHp());
			getOwner().sendPlayerInfo = true;
			ThreadPoolManager.getInstance().executeTask(new Runnable(){
				@Override
				public void run() {
					getOwner().savePlayer();
				}});
			return true;
		}
		return false;
	}

	public Player getOwner() {
		return (Player) super.getOwner();
	}

	public boolean addExp(long value) {
		getOwner().sendPlayerInfo = true;
		return super.addExp(value);
	}

	public void setCurrentHp(int value) {
		super.setCurrentHp(value);
		getOwner().sendPlayerInfo = true;
	}

	public void setBreath(int value) {
		super.setBreath(value);
		getOwner().sendPlayerInfo = true;
	}

	@Override
	public void calcStats() {
		super.calcStats();
		// защита = одетая броня + CON /10;
		// System.out.println("armor "+ getOwner().inv.armor);
		// System.out.println("damage "+ getOwner().getWeapon().damage);
		setDefenseRate(getOwner().inv.armor + getBaseDefense() + getCon() / 10);
		// System.out.println("1 "+ getOwner());
		// System.out.println("2 "+ getOwner().getWeapon());
		setAttackPower(getOwner().getWeapon().damage
				+ (getBaseAttack() / 100f * 0.1f) * getStr());
		float critPower = (getOwner().getWeapon().critRate + 0.5f * ((getDex() / 20)));
		setCriticalPower(critPower < 5 ? critPower : 5);
		setCriticalChance(getOwner().getWeapon().critChance);
		getOwner().sendPlayerInfo = true;
	}

}
